
import SQLite

class DbConversation : DbBase {
    var conversation: Table = Table("conversation")
    let _id = Expression<Int64>("id")
    let _type = Expression<Int>("type")
    let _targetId = Expression<String>("targetId")
    let _lastMessage = Expression<Int>("lastMessage")
    let _newestMessage = Expression<String>("newestMessage")
    let _timestamp = Expression<Int64>("timestamp")
    let _draft = Expression<String>("draft")
    let _unreadCount = Expression<Int>("unreadCount")
    let _unreadMention = Expression<Int>("unreadMention")
    let _unreadMentionAll = Expression<Int>("unreadMentionAll")
    let _isTop = Expression<Bool>("isTop")
    let _isSilent = Expression<Bool>("isSilent")
    
    func open(){
        do{
            try getDb()!.run(conversation.create(ifNotExists: true) { t in
                t.column(_id, primaryKey: .autoincrement)
                t.column(_type)
                t.column(_targetId, unique: true)
                t.column(_lastMessage)
                t.column(_newestMessage)
                t.column(_timestamp)
                t.column(_draft)
                t.column(_unreadCount)
                t.column(_unreadMention)
                t.column(_unreadMentionAll)
                t.column(_isTop)
                t.column(_isSilent)
            })
            try getDb()!.run(conversation.createIndex(_targetId, ifNotExists: true))
        }catch{
            print("\(error)")
        }
    }
    func toInfo(_ row:Row) -> DMCCConversation {
        let u = DMCCConversation()
        u.id = row[_id]
        u.type = OsnConversationType(rawValue: row[_type])
        u.targetId = row[_targetId]
        u.lastMessage = row[_lastMessage]
        u.newestMessage = row[_newestMessage]
        u.draft = row[_draft]
        u.unread = row[_unreadCount]
        u.unreadMention = row[_unreadMention]
        u.unreadMentionAll = row[_unreadMentionAll]
        u.isTop = row[_isTop]
        u.isSilent = row[_isSilent]
        u.timestamp = row[_timestamp]
        return u
    }
    func getConversationInfo(_ osnId:String) -> DMCCConversation? {
        do{
            for convs in try getDb()!.prepare(conversation.filter(osnId == _targetId)) {
                return toInfo(convs)
            }
        }catch{
            print("\(error)")
        }
        return nil
    }
    func addConversationInfo(_ info:DMCCConversation) -> Bool {
        do{
            return try getDb()!.run(conversation.upsert(
                _type <- info.type.rawValue,
                _targetId <- info.targetId,
                _lastMessage <- info.lastMessage,
                _newestMessage <- info.newestMessage,
                _timestamp <- info.timestamp,
                _draft <- info.draft,
                _unreadCount <- info.unread,
                _unreadMention <- info.unreadMention,
                _unreadMentionAll <- info.unreadMentionAll,
                _isTop <- info.isTop,
                _isSilent <- info.isSilent,
                onConflictOf: _targetId)) >= 0
        }catch{
            print("\(error)")
        }
        return false
    }
    func delConversationInfo(_ osnId:String) ->Bool {
        do{
            return try getDb()!.run(conversation.filter(_targetId == osnId).delete()) > 0
        }catch{
            print("\(error)")
        }
        return false
    }
    func listConversationInfo(_ begin:Int, _ count:Int) ->[DMCCConversation] {
        var ms:[DMCCConversation] = []
        do{
            for cs in try getDb()!.prepare(conversation.filter(_id > Int64(begin)).limit(count)) {
                let u = toInfo(cs)
                ms.append(u)
            }
        }catch{
            print("\(error)")
        }
        return ms
    }
    func listConversationInfo(_ type:OsnConversationType)->[DMCCConversation]{
        var ms:[DMCCConversation] = []
        do{
            for cs in try getDb()!.prepare(conversation.filter(_type == type.rawValue)) {
                let u = toInfo(cs)
                ms.append(u)
            }
        }catch{
            print("\(error)")
        }
        return ms
    }
}
let dbConversation = DbConversation()
